//
// Created by 11510 on 2024/6/18.
//
#include <iostream>

using namespace std;

class Base {
public:
    Base() : _base(new int(10)) {
        cout << "Base()" << endl;
    }

    virtual void display() const {
        cout << "*_base:" << *_base << endl;
    }

    /*virtual*/ ~Base() {
        cout << "~Base()" << endl;
        if (_base) {
            delete _base;
            _base = nullptr;
        }

    }

private:
    int *_base;
};

class Derived : public Base {
public:
    Derived() : Base(), _derived(new int(20)) {
        cout << "Derived()" << endl;
    }

    virtual void display() const override {
        cout << "*_derived:" << *_derived << endl;
    }

    ~Derived() {
        if (_derived) {
            delete _derived;
            _derived = nullptr;
        }
        cout << "~Derived()" << endl;
    }

private:
    int *_derived;
};

void test0() {
    Base *pbase = new Derived();
    pbase->display();
    /* 此处有内存泄漏，只调用了父类的析构函数，没有调用子类的析构函数 */
    delete pbase;

}

int main() {
    test0();
}